#include "PoseEstimationModel.h"

#include "core/AppDirectory.h"

namespace {
const QString UserFilename = "pcl.pose_estimation_model.json";
}

PoseEstimationModel::PoseEstimationModel()
    : BasePclModel()
{
    setPclHelpPage("https://pcl.readthedocs.io/projects/tutorials/en/master/alignment_prerejective.html");
}

PoseEstimationModel& PoseEstimationModel::get()
{
    static PoseEstimationModel instance;
    if (instance.filename().isEmpty()) {
        instance.setFilename(AppDirectory::get().getAppDocFile(UserFilename));
        instance.load();
    }
    return instance;
}

void PoseEstimationModel::fromJson(const QJsonObject& json, QStringList& errorList)
{

    BasePclModel::fromJson(json, errorList);
    pcdFile_ = json["pcdFile"].toString(pcdFile_);
    pcdFile2_ = json["pcdFile2"].toString(pcdFile2_);
    leafSize_ = json["leafSize"].toDouble(leafSize_);
    nestRadiusSearch_ = json["nestRadiusSearch"].toDouble(nestRadiusSearch_);
    festRadiusSearch_ = json["festRadiusSearch"].toDouble(festRadiusSearch_);
    alignMaximumIterations_ = json["alignMaximumIterations"].toInt(alignMaximumIterations_);
    alignNumberOfSamples_ = json["alignNumberOfSamples"].toInt(alignNumberOfSamples_);
    alignCorrespondenceRandomness_ = json["alignCorrespondenceRandomness"].toInt(alignCorrespondenceRandomness_);
    alignSimilarityThreshold_ = json["alignSimilarityThreshold"].toDouble(alignSimilarityThreshold_);
    alignMaxCorrespondenceDistance_ = json["alignMaxCorrespondenceDistance"].toDouble(alignMaxCorrespondenceDistance_);
    alignInlierFraction_ = json["alignInlierFraction"].toDouble(alignInlierFraction_);
    sceneCloudPointColor_ = QColor(json["sceneCloudPointColor"].toString(sceneCloudPointColor_.name()));
    sceneCloudPointSize_ = json["sceneCloudPointSize"].toInt(sceneCloudPointSize_);
    objectAlignedCloudPointColor_ =
        QColor(json["objectAlignedCloudPointColor"].toString(objectAlignedCloudPointColor_.name()));
    objectAlignedCloudPointSize_ = json["objectAlignedCloudPointSize"].toInt(objectAlignedCloudPointSize_);
}

QJsonObject PoseEstimationModel::toJson() const
{
    QJsonObject json = BasePclModel::toJson();

    json.insert("pcdFile", QJsonValue(pcdFile_));
    json.insert("pcdFile2", QJsonValue(pcdFile2_));
    json.insert("leafSize", QJsonValue(leafSize_));
    json.insert("nestRadiusSearch", QJsonValue(nestRadiusSearch_));
    json.insert("festRadiusSearch", QJsonValue(festRadiusSearch_));
    json.insert("alignMaximumIterations", QJsonValue(alignMaximumIterations_));
    json.insert("alignNumberOfSamples", QJsonValue(alignNumberOfSamples_));
    json.insert("alignCorrespondenceRandomness", QJsonValue(alignCorrespondenceRandomness_));
    json.insert("alignSimilarityThreshold", QJsonValue(alignSimilarityThreshold_));
    json.insert("alignMaxCorrespondenceDistance", QJsonValue(alignMaxCorrespondenceDistance_));
    json.insert("alignInlierFraction", QJsonValue(alignInlierFraction_));
    json.insert("sceneCloudPointColor", QJsonValue(sceneCloudPointColor_.name()));
    json.insert("sceneCloudPointSize", QJsonValue(sceneCloudPointSize_));
    json.insert("objectAlignedCloudPointColor", QJsonValue(objectAlignedCloudPointColor_.name()));
    json.insert("objectAlignedCloudPointSize", QJsonValue(objectAlignedCloudPointSize_));

    return json;
}
